package com.cx.util.device;

import android.content.Intent;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Build;

import com.cx.util.AppUtils;

import java.io.File;

public class IntentHelper {


    /**
     * get install app intent
     *
     * @param file the location app file
     * @return Intent
     */
    public static Intent getInstallIntent(File file) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        String type = "application/vnd.android.package-archive";
        Uri uri = UriHelper.getFileUri(file);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        } else {
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        intent.setDataAndType(uri, type);
        return intent;
    }


    /**
     * get a intent to call phone
     *
     * @param phone phone
     * @return Intent
     */
    public static Intent getPhoneIntent(String phone) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("tel:" + phone));
        return intent;
    }


    /**
     * ensure device has activity can response intent
     *
     * @param intent intent
     * @return true  false
     */
    public boolean resolveActivity(Intent intent) {
        return intent.resolveActivity(AppUtils.getApp().getPackageManager()) != null;
    }


    /**
     * 保存图片到相册
     *
     * @param imageFile imageFile
     */
    public static void saveToMediaStore(File imageFile) {
        MediaScannerConnection.scanFile(AppUtils.getApp(),
                new String[]{imageFile.toString()},
                new String[]{imageFile.getName()}, null);

    }
}
